package com.xjnt.config;

import com.jfinal.plugin.druid.DruidPlugin;

public class InitialConfig {
	
	private GlobalConfig config;

	public InitialConfig(GlobalConfig config) {
		config.loadPropertyFile("config.properties");
		this.config = config;
	}
	
	public boolean getDevMode(){
		return this.config.getPropertyToBoolean("sys.devMode");
	}
	
	public String getViewPath(){
		return this.config.getProperty("sys.viewPath");
	}
	
	public String getDbType(){
		return this.config.getProperty("sys.dbType");
	}
	
	public boolean getShowSql(){
		return this.config.getPropertyToBoolean("sys.showSql");
	}
	
	public String[] getModelScanPkg(){
		String scanPkg = this.config.getProperty("sys.model.scanPkg", "");
		return scanPkg.split(",");
	}
	
	public String[] getModelScanJar(){
		String scanJar = this.config.getProperty("sys.model.scanJar", "");
		return scanJar.split(",");
	}
	
	public String[] getServiceScanPkg(){
		String scanPkg = this.config.getProperty("sys.service.scanPkg", "");
		return scanPkg.split(",");
	}
	
	public String[] getServiceScanJar(){
		String scanJar = this.config.getProperty("sys.service.scanJar", "");
		return scanJar.split(",");
	}
	
	public String[] getControllerScanPkg(){
		String scanPkg = this.config.getProperty("sys.controller.scanPkg", "");
		return scanPkg.split(",");
	}
	
	public String[] getControllerScanJar(){
		String scanJar = this.config.getProperty("sys.controller.scanJar", "");
		return scanJar.split(",");
	}
	
	public String[] getQuartzJobScanPkg(){
		String scanPkg = this.config.getProperty("sys.quartz.job.scanPkg", "");
		return scanPkg.split(",");
	}
	
	public String[] getQuartzJobScanJar(){
		String scanJar = this.config.getProperty("sys.quartz.job.scanJar", "");
		return scanJar.split(",");
	}
	
	public DruidPlugin getDruidPlugin(){
		DruidPlugin druidPlugin = new DruidPlugin(
				this.config.getProperty("mysql.jdbc.url"), 
				this.config.getProperty("mysql.jdbc.user"),
				this.config.getProperty("mysql.jdbc.password"),
				this.config.getProperty("mysql.jdbc.driverClass"));
		int initialSize = this.config.getPropertyToInt("jdbc.druid.initialSize");
		int minIdle = this.config.getPropertyToInt("jdbc.druid.minIdle");
		int maxActive = this.config.getPropertyToInt("jdbc.druid.maxActive");
		druidPlugin.set(initialSize, minIdle, maxActive);
		
		long timeBetweenEvictionRunsMillis = this.config.getPropertyToLong("jdbc.druid.timeBetweenEvictionRunsMillis");
		long minEvictableIdleTimeMillis = this.config.getPropertyToLong("jdbc.druid.minEvictableIdleTimeMillis");
		int maxPoolPreparedStatementPerConnectionSize = this.config.getPropertyToInt("jdbc.druid.maxPoolPreparedStatementPerConnectionSize");
		druidPlugin.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		druidPlugin.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		druidPlugin.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
		
		// 增加状态监控
		//druidPlugin.addFilter(new StatFilter());
		// 指定数据库类型
		//WallFilter wallFilter = new WallFilter();
		//wallFilter.setDbType(getDbType().toLowerCase());
		//druidPlugin.addFilter(wallFilter);
		return druidPlugin;
	}
}
